import { Component, OnInit } from '@angular/core';
import { ExtractServiceService } from './extract-service.service';
import { Router, NavigationEnd, ActivatedRoute } from '@angular/router';
import { DataService } from '../../../../services/data.service';
import * as _ from 'lodash';
@Component({
  selector: 'app-extract-service',
  templateUrl: './extract-service.component.html',
  styleUrls: ['./extract-service.component.scss'],
  providers: [ExtractServiceService]
})
export class ExtractServiceComponent implements OnInit {
  //从list获取到的单条条目信息
  public extractInfo: any;
  //保存条目post数据
  public addEntryPostBody: any = {
    "sWeekly": "",//周报
    "sMonth": "",//月报
    "sDaily": "",//季报
    "sYear": "",//年报
    "sAchAss": "",//绩效评估报告
    "sMajorMatters": "",//重大事项
    "sRemind": "",//净值大幅波动提醒
    "sStopSituation": "",//终止情况
    "sLiqReport": "",//清算报告
  }
  // 克隆一份数据，用于对比标红
  public cloneAddEntryPostBody: any = {
    sWeekly: "",
    sMonth: "",
    sDaily: "",
    sYear: "",
    sAchAss: "",
    sMajorMatters: "",
    sRemind: "",
    sStopSituation: "",
    sLiqReport: "",
  }
  //服务条目模板参数
  public serviceEntryModel: any;
  constructor(
    public extractServiceService: ExtractServiceService,
    public router: Router,
    public dataService: DataService
  ) { }

  ngOnInit() {
    if (this.extractInfo.sServiceItem == '1' || this.extractInfo.sServiceItem == '3') {
      this.getSingleServiceInfo();
    } else {
      //调用获取默认模板接口
      this.getServiceEntryModel();
    }
  }
  //保存条目
  saveEntry() {
    let that = this;
    window["swal"]({
      title: "注意",
      text: "是否确定保存条目",
      type: "warning",
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      showCancelButton: true,
      closeOnConfirm: false,
      closeOnCancel: true,
      showLoaderOnConfirm: true
    },
      function (isConfirm) {
        if (isConfirm) {
          if (that.extractInfo.sServiceItem == '1' || that.extractInfo.sServiceItem == '3') {
            that.editServiceInfo();
            that.dataService.entryEmitter.emit('extractService');
          } else {
            that.addEntryPostBody['sItemStatus'] = "1";
            that.extractServiceService.save(that.addEntryPostBody, that.extractInfo.nFundNo).subscribe(data => {
              if (data) {
                that.dataService.entryEmitter.emit('extractService');
                window['swal']("提示", "新增条目成功!", "success");
              }
            })
          }
        }
      })
  }
  //查询-单个服务条目条目内容（根据FundNo）
  getSingleServiceInfo() {
    let that = this;
    this.extractServiceService.getSingleInfo(this.extractInfo.nFundNo).subscribe(data => {
      if (data) {
        this.addEntryPostBody = data;
        this.cloneAddEntryPostBody = _.cloneDeep(data);
        setTimeout(function () {
          that.resetTriangle();
        }, 1);
      }
    })
  }
  //更新服务类条目基本信息(不需要审核)
  editServiceInfo() {
    this.extractServiceService.edit(this.addEntryPostBody).subscribe(data => {
      if (data) {
        window['swal']("提示", "修改条目成功!", "success")
      }
    })
  }
  //获取模板参数接口
  getServiceEntryModel() {
    let that = this;
    this.extractServiceService.getServiceEntryModel().subscribe(data => {
      if (data) {
        this.serviceEntryModel = data;
        _.forEach(this.serviceEntryModel, item => {
          var sItemValue = item.sItemValue;
          switch (item.sRemark) {
            case '周报':
              that.addEntryPostBody.sWeekly = sItemValue;
              that.cloneAddEntryPostBody.sWeekly = _.cloneDeep(sItemValue);
              break;
            case '月报':
              that.addEntryPostBody.sMonth = sItemValue;
              that.cloneAddEntryPostBody.sMonth = _.cloneDeep(sItemValue);
              break;
            case '季报':
              that.addEntryPostBody.sDaily = sItemValue;
              that.cloneAddEntryPostBody.sDaily = _.cloneDeep(sItemValue);
              break;
            case '年报':
              that.addEntryPostBody.sYear = sItemValue;
              that.cloneAddEntryPostBody.sYear = _.cloneDeep(sItemValue);
              break;
            case '绩效评估报告':
              that.addEntryPostBody.sAchAss = sItemValue;
              that.cloneAddEntryPostBody.sAchAss = _.cloneDeep(sItemValue);
              break;
            case '重大事项':
              that.addEntryPostBody.sMajorMatters = sItemValue;
              that.cloneAddEntryPostBody.sMajorMatters = _.cloneDeep(sItemValue);
              break;
            case '净值大幅波动提醒':
              that.addEntryPostBody.sRemind = sItemValue;
              that.cloneAddEntryPostBody.sRemind = _.cloneDeep(sItemValue);
              break;
            case '终止情况':
              that.addEntryPostBody.sStopSituation = sItemValue;
              that.cloneAddEntryPostBody.sStopSituation = _.cloneDeep(sItemValue);
              break;
            case '清算报告':
              that.addEntryPostBody.sLiqReport = sItemValue;
              that.cloneAddEntryPostBody.sLiqReport = _.cloneDeep(sItemValue);
              break;
          }
        })
        setTimeout(function () {
          that.resetTriangle();
        }, 1);
      }
    })
  }
  // 修改标红,val为改变后的值,name为改变的字段
  editChange(val: any, name: any) {
    let that = this;
    // 定位输入框
    let id = "#" + name;
    // 循环对象
    _.forIn(this.cloneAddEntryPostBody, (value, key) => {
      // 判断字段名是否相等
      if (key == name) {
        // 判断原值是否为null或''(后端空值返回null,前端空值为'',实际都为空,相等)
        if (value == null || value == '') {
          if (val == null || val == '') {
            window['$'](id).removeClass('triangle-topleft-red');
          } else {
            window['$'](id).addClass('triangle-topleft-red');
          }
        } else {
          // 判断原值不为null,直接对比
          if (value == val) {
            window['$'](id).removeClass('triangle-topleft-red');
          } else {
            window['$'](id).addClass('triangle-topleft-red');
          }
        }
      }
    })
  }
  resetTriangle() {
    window['$']('#sWeekly').removeClass('triangle-topleft-red');
    window['$']('#sMonth').removeClass('triangle-topleft-red');
    window['$']('#sDaily').removeClass('triangle-topleft-red');
    window['$']('#sYear').removeClass('triangle-topleft-red');
    window['$']('#sAchAss').removeClass('triangle-topleft-red');
    window['$']('#sMajorMatters').removeClass('triangle-topleft-red');
    window['$']('#sRemind').removeClass('triangle-topleft-red');
    window['$']('#sStopSituation').removeClass('triangle-topleft-red');
    window['$']('#sLiqReport').removeClass('triangle-topleft-red');
  }
}
